<%--
  Created by IntelliJ IDEA.
  User: abb
  Date: 2018-07-08
  Time: 14:43
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" type="text/css" href="/static/js/jquery-easyui/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="/static/js/jquery-easyui/themes/icon.css">
    <script type="text/javascript" src="/static/js/jquery-easyui/jquery.min.js"></script>
    <script type="text/javascript" src="/static/js/jquery-easyui/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="/static/js/jquery-easyui/locale/easyui-lang-zh_CN.js"></script>
    <script>
        $(function () {
            $('#deptDataGrid').datagrid({
                url:'/department/list',
                columns:[[
                    {field:'id',title:'部门编号',width:100},
                    {field:'sn',title:'部门编码',width:100},
                    {field:'name',title:'部门名称',width:100},
                    {field:'manager',title:'部门经理',width:100,formatter:formattermanager},
                    {field:'parent',title:'上级部门',width:100,formatter:formatterparent},
                    {field:'status',title:'部门状态',width:100,formatter:formatterStatus}

                ]],
                fitColumns:true,
                fit:true,
                pagination:true,
                singleSelect:true,
                toolbar:"#departmentToolBar"
            });
            $('#deptDialog').dialog({
                title: 'dept',
                width: 400,
                height: 400,
                closed: true,
                buttons:"#deptDialogButtons"

            });






        })
        function formattermanager(value,row,index) {
            return value?value.username:"";
        }
        function formatterparent(value,row,index) {
            return value?value.name:"";
        }
        function formatterStatus(value,row,index) {
            return value==0?"<span style='color:green'>正常</span>":"<span style='color:red'>撤销</span>";
        }
        function save2() {
            var url = "/department/save";

            //获取员工的id
            var employeeId = $("#deptId").val();
            if(employeeId){
                //编辑的url
                url = "/department/update";
            }
            //提交表单
            $("#deptDialogForm").form("submit",{
                url:url,
                onSubmit:function (param) {
                    //拿到选中的所有角色
                    var values = combobox_role.combobox("getValues");
                    //遍历拼接成  :   roles[0].id = xx
                    for(var i = 0 ; i<values.length ; i++){
                        var id = values[i];
                        //给请求拼接角色的参数
                        param['roles['+i+'].id'] = id;
                    }
                },
                success:function (data) {
                    data = $.parseJSON(data);
                    if(data.success){
                        $.messager.alert("提示","操作成功","info",function () {
                            cmdObj.cancel();
                            cmdObj.reloadEmp();
                        });
                    }else{
                        $.messager.alert("提示",data.msg,"error");
                    }
                }
            });
        }
        function saveDept() {
            var url = "/department/save";
            if($("#deptId").val()){
                url = "/department/update";
            }

            $("#deptDialogForm").form('submit', {
                    url: url,

            success: function(data){
                 data = $.parseJSON(data);
                if(data.success){
                    $.messager.alert("提示","成功","info",function () {
                        cancel();
                        reload();
                    })
                }else{
                    $.messager.alert("提示",data.msg,"error");
                }
            }
        });

        }
        function cancel() {
            $("#deptDialog").dialog("close");
        }
        function add() {
            $("#deptDialogForm").form("clear");
            $("#deptDialog").dialog("setTitle","新部门");
            $("#deptDialog").dialog("open");
            
        }

        function edit() {

            var department = $('#deptDataGrid').datagrid("getSelected");

            if (!department) {
                $.messager.alert('提示', '请选中一条记录！', 'info');
                return;
            }

            $("#deptDialogForm").form("clear");
            $("#deptDialog").dialog("setTitle", "部门编辑");

            if (department.manager) {
                department["manager.id"] = department.manager.id;
            }

            if (department.parent) {
                department["parent.id"] = department.parent.id;
            }

            var deptId = department.id;


            $("#deptDialogForm").form("load", department);

            $("#deptDialog").dialog("open");
        }



        function closeDept() {

         var select = $("#deptDataGrid").datagrid("getSelected");
            if(select == null){
                $.messager.alert("提示","请选择一行","info");
                return;
            }
            $.messager.confirm("提示","确定要撤销吗",function (yes) {
                if(yes){
                    $.post("/department/close",{"id":select.id},function (data) {
                        if(data.success){
                            $.messager.alert("提示","成功","info",function () {
                                reload();
                            })
                        }else{
                            $.messager.alert("提示",data.msg,"error");
                        }
                    })
                }
            })
        }
        function reload() {
            $("#deptDataGrid").datagrid("reload");
        }


    </script>
</head>
<body>
<div id="deptDialog">
    <form id="deptDialogForm" method="post">
        <input type="hidden" name="id" id="deptId">
        <table>
            <tr>
                <td>部门编码</td>
                <td>
                    <input class="easyui-textbox" style="width:300px" name="sn">


                </td>
            </tr>
            <tr>
                <td>部门名称</td>
                <td>
                    <input class="easyui-textbox" style="width:300px" name="name">
                </td>
            </tr>
            <tr>
                <td>部门经理</td>

                <td><input id="deptDialogCombobox" class="easyui-combobox" name="manager.id"
                           data-options="valueField:'id',textField:'username',url:'/employee/listEmployee'" />   </td>

            </tr>
            <tr>
                <td>上级部门</td>
                <td>
                    <input class="easyui-textbox" style="width:300px" name="parent.id">
                </td>
            </tr>
        </table>
    </form>
</div>
<div id="deptDialogButtons">
    <a class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="saveDept()">确定</a>
    <a class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="cancel()">取消</a>
</div>
<div id="deptToolBar">
    <a class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="add()">新增</a>
    <a class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true" onclick="edit()">编辑</a>
    <a class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="closeDept()">撤销</a>
    <a class="easyui-linkbutton" data-options="iconCls:'icon-reload',plain:true" onclick="reload()">刷新</a>
</div>


<table id="deptDataGrid"></table>

</body>
</html>
